package com.itdaan.portal.common.util;

/**
 * Unicode转中文工具类
 * @author www.itdaan.com
 */
public class UnicodeToChinese {
	
    private UnicodeToChinese(){
    }
    
    public static UnicodeToChinese getInstance(){
        return new UnicodeToChinese();
    }
    
    public String convert(String parameterString){
        StringBuffer buffer = new StringBuffer();
        if(parameterString.indexOf("\\u")==-1){
            buffer.append(parameterString);
        }else{
            while(parameterString.indexOf("\\u")!=-1){
                String substringNotUnicode = parameterString.substring(0,parameterString.indexOf("\\u"));
                buffer.append(substringNotUnicode);
                String substringOfUnicode="";
                try {
                    substringOfUnicode = parameterString.substring(parameterString.indexOf("\\u"),parameterString.indexOf("\\u")+6);
                } catch (StringIndexOutOfBoundsException e) {
                    return buffer.append(parameterString.substring(parameterString.indexOf("\\u"))).toString();
                }
                try {
                    buffer.append((char)Integer.parseInt(substringOfUnicode.substring(2), 16));
                } catch (NumberFormatException e) {
                    buffer.append(substringOfUnicode);
                }
                parameterString= parameterString.substring(parameterString.indexOf("\\u")+6);
            }
            buffer.append(parameterString);
        }
        return buffer.toString();
    }
	
    public static void main(String[] args) {
    	System.out.println(UnicodeToChinese.getInstance().convert("\u6211\u5df2\u7ecf\u8bfb\u5230\uff0c\u5982\u679c\u6709\u5f88\u591a\u7684\u5f00\u9500\uff0c\u4f7f\u8fde\u63a5\u5230\u6211\u7684\u5206\u8d1d\u6bcf\u4e2a\u4f11\u606f\u547c\u53eb\uff0c\u7136\u540e\u6211\u9700\u8981\u505a\u4e00\u4e2a\u6301\u4e45\u7684\u8fde\u63a5\uff0c\u6709\u4eba\u4f7f\u7528\u8fd9\u4e2a\u4f11\u606f\u3002\u5728\u6211\u7684\u8def\u7531\u5668\u91cc\u505a\u8fd9\u4ef6\u4e8b\u7684\u6b63\u786e\u65b9\u6cd5\u662f\u4ec0\u4e48\uff1f\u73b0\u5728\u6bcf\u4e00\u4e2a\u8bf7\u6c42\u88ab\u63a5\u6536\u7684\u8bf7\u6c42\u90fd\u4f1a\u6253\u5f00\u4e00\u4e2a\u8fde\u63a5\uff0c\u8bbf\u95ee\u8be5\u6570\u636e\u5e93\u7136\u540e\u5173\u95ed\u8fde\u63a5\u3002"));
	}
    
}
